# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import time
import re
import csv
import os
from pymongo import MongoClient


requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

"""
华为应用市场
https://appgallery.huawei.com/#/Featured
"""


class Huawei_Get():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        # requests.utils.dict_from_cookiejar(html.cookies)  # 获取cookies转dict
        # res.cookies.get_dict()  # 获取cookies
        self.cookie = cookiejar.CookieJar()
        # self.ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        # self.ua = UserAgent(verify_ssl=False)  #
        self.headers = {
            'Accept': 'application/json, text/plain, */*',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36'

        }

    # 应用排行榜 400
    def get_top400(self, cursor):
        postData = {
            'method': 'internal.getTabDetail',
            'serviceType': '20',
            'reqPageNum': cursor,
            'uri': '14fccca02f924f0e9b5b9c472ddbba4e',
            'maxResults': '25',
            'zone':'',
            'locale': 'zh'
        }
        url = f"https://web-drcn.hispace.dbankcloud.cn/uowap/index"

        html = HttpUtils.do_request("GET", url, self.headers, postData)
        html.encoding = "utf-8"
        data_json = json.loads(html.text)
        count = data_json['count']  # 数量
        totalPages = data_json['totalPages']  # 总页数

        for d in data_json['layoutData'][0]['dataList']:
            dict_data = dict()
            dict_data['app名称'] = d['name']
            dict_data['类别'] = d['tagName']
            dict_data['简介'] = d.get('memo')
            dict_data['几颗星'] = d['stars']
            dict_data['评分'] = d['score']
            dict_data['安装次数'] = d['downCountDesc']
            dict_data['文件大小'] = d['sizeDesc']
            dict_data['使用年龄'] = d['minAge']

            print(str(dict_data))

        # 翻页
        if cursor < totalPages:
            print(f"=====共【{totalPages}】页=======第【{cursor + 1}】页=============")
            self.get_top400(cursor + 1)

    # 评论 能提取4页 100条
    def get_comments(self, cursor):
        postData = {
            'method': 'internal.user.commenList3',
            'serviceType': '20',
            'reqPageNum': cursor,
            'maxResults': '25',
            'appid': 'C10505945',  # 应用id
            'version': '10.0.0',
            'zone':'',
            'locale': 'zh'
        }
        url = f"https://web-drcn.hispace.dbankcloud.cn/uowap/index"

        html = HttpUtils.do_request("GET", url, self.headers, postData)
        html.encoding = "utf-8"
        data_json = json.loads(html.text)
        count = data_json['count']  # 评论数量
        totalPages = data_json['totalPages']  # 总页数
        if cursor == 1:
            score = data_json['score']  # 评分
        for d in data_json['list']:
            dict_data = dict()
            dict_data['用户名'] = d['nickName']
            dict_data['终端名称'] = d['phone']
            dict_data['评论时间'] = d['operTime']
            dict_data['版本号'] = d['versionName']
            dict_data['几颗星'] = d['stars']
            dict_data['评论内容'] = d['commentInfo']

            print(str(dict_data))

        # 翻页
        if cursor < totalPages:
            print(f"=====共【{totalPages}】页=======第【{cursor + 1}】页=============")
            self.get_comments(cursor + 1)

        # print(data_json)


if __name__ == '__main__':
    # conn = MongoClient('127.0.0.1', 27017)
    # db = conn["Test"]
    huwei_get = Huawei_Get()

    # print(f"============第【{i}】页=============")
    # 获取评论
    huwei_get.get_comments(1)

    # 获取排行400榜
    # huwei_get.get_top400(1)
